﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Build.Client;

namespace PsTFS.Objects.Methodes
{
    /// <summary>
    /// Classe de service permettant de retourner les services de Team Foundation Server
    /// </summary>
    public class Methode_Service
    {
        /// <summary>
        /// Objet contenant le Version Control Server
        /// </summary>
        private static VersionControlServer vcs = null;
        /// <summary>
        /// Objet contenant le WorkItem Store
        /// </summary>
        private static WorkItemStore wis = null;


        #region IGroupSecurityService

        /// <summary>
        /// Methode retournant le IGroupeSecurityService qui permet de travailer sur la gestion de la sécurité de TFS
        /// </summary>
        /// 
        /// <returns></returns>
        public static IGroupSecurityService IGSS()
        {
            return (IGroupSecurityService)Context.GetContext().Tfs.GetService(typeof(IGroupSecurityService));
        }

        #endregion IGroupSecurityService

        /// <summary>
        /// Service TFS
        /// </summary>
        /// <returns></returns>
        public static ICommonStructureService ICSS()
        {
            return (ICommonStructureService)Context.GetContext().Tfs.GetService(typeof(ICommonStructureService));

        }

        #region VersionControlServer

        /// <summary>
        /// Permet de récupérer le version contrôle server et de rendre générique la gestion des events
        /// </summary>
        /// 
        /// <returns>Le version control Server</returns>
        public static VersionControlServer VCS()
        {
            if (vcs != null)
            {
                if (vcs.TeamFoundationServer.Name != Context.GetContext().Tfs.Name)
                {
                    vcs = null;
                }
            }
            if (vcs == null)
            {
                vcs = (VersionControlServer)Context.GetContext().Tfs.GetService(typeof(VersionControlServer));

                Context.GetContext().User = vcs.TeamFoundationServer.AuthenticatedUserName;
            }

            return vcs;
        }

        #endregion VersionControlServer

        #region WorkItemStore

        /// <summary>
        /// Objet retournant le workitem store avec un TFS précisé
        /// </summary>
        /// <returns>Le service de WorkItem</returns>
        public static WorkItemStore WIS()
        {
            if (wis != null)
            {
                if (wis.TeamFoundationServer.Name != Context.GetContext().Tfs.Name)
                {
                    wis = null;
                }
            }

            if (wis == null)
            {

                wis = (WorkItemStore)Context.GetContext().Tfs.GetService(typeof(WorkItemStore));
            }
                
            return wis;
            
        }

        #endregion WorkItemStore

        /// <summary>
        /// Service pour la gestion des Build
        /// </summary>
        /// <returns></returns>
        public static IBuildServer IBS()
        {
            return (IBuildServer)Context.GetContext().Tfs.GetService(typeof(IBuildServer));
        }
    }
}
